package net.toocruel.iqismart.web.controller.core;

import net.toocruel.iqismart.entity.constants.SourceType;
import net.toocruel.iqismart.entity.core.Answer;
import net.toocruel.iqismart.entity.core.Comment;
import net.toocruel.iqismart.entity.core.Question;
import net.toocruel.iqismart.entity.setting.Setting;
import net.toocruel.iqismart.service.UserService;
import net.toocruel.iqismart.service.core.*;
import net.toocruel.iqismart.service.exception.UserRepeatException;
import net.toocruel.iqismart.web.controller.base.AbstractBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Controller
@RequestMapping("answer")
public class AnswerController extends AbstractBaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private TagService tagService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private AnswerService answerService;

    @GetMapping("edit/{id}")
    public ModelAndView edit(@PathVariable  Long id,HttpServletRequest request){

        Answer answer = answerService.findById(id);

        //seoTitle
        request.setAttribute("seoTitle","编辑回答 - "+Setting.SETTING.getSeo().getQuestionTitle());

        return new ModelAndView(getTemplate("edit"),"answer",answer);
    }

    @PostMapping("edit/{id}")
    public ModelAndView edit(@PathVariable  Long id,@Valid Answer answer, BindingResult bindingResult,
                            HttpServletRequest request, HttpServletResponse response){

        Question question = answerService.findById(answer.getId()).getQuestion();
        answer.setQuestion(question);

        if(!bindingResult.hasErrors()){
            try {

                answerService.update(answer);

                return new ModelAndView("redirect:/question/"+answer.getQuestion().getId());
            } catch (UserRepeatException e) {
                e.printStackTrace();
                request.setAttribute("error",e);
            }
        }


        ModelAndView modelAndView = new ModelAndView(getTemplate("edit"), "answer", answer);
        return modelAndView;
    }

    /**
     * 采纳
     * @param id
     * @param request
     * @return
     */
    @GetMapping("adopt/{id}")
    public ModelAndView adopt(@PathVariable  Long id,HttpServletRequest request){

        answerService.adopt(id);

        return new ModelAndView("redirect:"+request.getHeader("referer"));
    }


    @GetMapping("{id}/comments")
    public ModelAndView comments(@PathVariable Long id,
                                 HttpServletRequest request,
                                 @RequestParam(defaultValue = "10000000") int size,
                                 @RequestParam(defaultValue = "1") int page,
                                 @RequestParam(defaultValue = "id") String sortBy,
                                 @RequestParam(defaultValue = "asc") String sortOrder){

        Page<Comment> commentPage = commentService.getCommonList(SourceType.answer, id, new PageRequest(page - 1, size, new Sort(Sort.Direction.fromString(sortOrder), sortBy)));

        return new ModelAndView(getTemplate("question","commentList"),"comments",commentPage.getContent());
    }

    @Override
    protected String getModule() {
        return "answer";
    }
}
